<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

        <!-- 表格顶部菜单 -->
        <!-- 自定义按钮请使用插槽，甚至公共搜索也可以使用具名插槽渲染，参见文档 -->
        <TableHeader
            :buttons="['refresh', 'add', 'edit', 'delete', 'comSearch', 'quickSearch', 'columnDisplay']"
            :quick-search-placeholder="t('Quick search placeholder', { fields: t('project.quick Search Fields') })"
        ></TableHeader>

        <!-- 表格 -->
        <!-- 表格列有多种自定义渲染方式，比如自定义组件、具名插槽等，参见文档 -->
        <!-- 要使用 el-table 组件原有的属性，直接加在 Table 标签上即可 -->
        <Table ref="tableRef"></Table>

        <!-- 表单 -->
        <PopupForm />
        <setFeeitem />
        <detail />
        <wfrecord />
    </div>
</template>

<script setup lang="ts">
import { onMounted, provide, ref } from 'vue'
import { useRouter } from 'vue-router'
import { useI18n } from 'vue-i18n'
import PopupForm from './popupForm.vue'
import { baTableApi } from '/@/api/common'
import { exportProject } from '/@/api/backend/index'
import { defaultOptButtons } from '/@/components/table'
import TableHeader from '/@/components/table/header/index.vue'
import Table from '/@/components/table/index.vue'
import baTableClass from '/@/utils/baTable'
import setFeeitem from './setFeeitem.vue'
import detail from './detail.vue'
import wfrecord from './wfrecord.vue'
import { auth, fullUrl } from '/@/utils/common'

defineOptions({
    name: 'project',
})
const $router = useRouter()
const { t } = useI18n()
const tableRef = ref()
const optButtons: OptButton[] = defaultOptButtons(['edit', 'delete'])
const buttons: OptButton[] = [
    {
        render: 'basicButton',
        name: 'info',
        title: '详情',
        text: '详情',
        type: 'success',
        icon: '',
        class: 'table-row-info',
        disabledTip: false,
        click: (row: TableRow, field: TableColumn) => {
            doAction(row, 'detail')
        },
    },
    {
        render: 'basicButton',
        name: 'info',
        title: '审批记录',
        text: '审批记录',
        type: 'warning',
        icon: '',
        class: 'table-row-info',
        disabledTip: false,
        click: (row: TableRow, field: TableColumn) => {
            doAction(row, 'wfrecord')
        },
        disabled:() => {
            return auth({name: '/admin/project/wfrecords'})
        }
    },
    {
        render: 'basicButton',
        name: 'info',
        title: '明细导出',
        text: '明细导出',
        type: 'danger',
        icon: '',
        class: 'table-row-info',
        disabledTip: false,
        click: (row: TableRow, field: TableColumn) => {
            doAction(row, 'export')
        },
        disabled:() => {
            return auth({name: '/admin/project/export'})
        }
    },
    {
        render: 'tipButton',
        name: 'info',
        title: '预算设置',
        text: '预算设置',
        type: 'primary',
        icon: '',
        class: 'table-row-info',
        disabledTip: false,
        display(row, field) {
            return [0, 2].includes(row.budget_audit_state)
        },
        click: (row: TableRow, field: TableColumn) => {
            doAction(row, 'feeitem')
        },
    },
    {
        render: 'basicButton',
        name: 'info',
        title: '预算审核中',
        text: '预算审核中',
        type: 'info',
        icon: '',
        class: 'table-row-info',
        disabledTip: false,
        display(row, field) {
            return row.budget_audit_state == 1
        },
        click: (row: TableRow, field: TableColumn) => {
            return false;
        },
    },
]

// 新按钮合入到默认的按钮数组
const optBtns = [...buttons, ...optButtons]

/**
 * baTable 内包含了表格的所有数据且数据具备响应性，然后通过 provide 注入给了后代组件
 */
const columns: [] = [
    { type: 'selection', align: 'center', operator: false },
    { label: t('project.work_date'), prop: 'work_date', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: false, sortable: false },
    { label: t('project.custom_name'), prop: 'custom_name', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
    { label: t('project.title'), prop: 'title', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
    { label: t('项目经理'), prop: 'manager.nickname', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE' },
    { label: t('project.member_ids'), prop: 'member.nickname', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: false },
    { label: t('project.custom_company'), prop: 'custom_company', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
    { label: t('project.date'), prop: 'date', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: false, sortable: false },
    { label: '状态', prop: 'state', align: 'center', render:'switch',replaceValue:{0: '关闭', 1:'开启'},  operatorPlaceholder: t('Fuzzy query'), operator: 'eq', sortable: false },
    { label: t('Operate'), align: 'center', width: 300, fixed:'right', render: 'buttons', buttons: optBtns, operator: false },
]

if(!auth({name:'/admin/project', subNodeName: '/admin/project/close'})){
    columns.splice(8,1)
}
const baTable = new baTableClass(
    new baTableApi('/admin/Project/'),
    {
        pk: 'id',
        column: columns,
        dblClickNotEditColumn: [undefined],
    },
    {
        defaultItems: { date: null },
    }
)

provide('baTable', baTable)

onMounted(() => {
    baTable.table.ref = tableRef.value
    baTable.mount()
    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
})

const doAction = (row: TableRow, opt: string) => {
    // baTable.form.extend!['userinfo'] = row;
    baTable.form.items! = {...row};
    baTable.form.operate = opt
}

const doExport = (row: TableRow) => {
    exportProject({project_id: row.id}).then((res: any) => {
        window.open(fullUrl(res.data.file))
    })
}
</script>

<style scoped lang="scss"></style>
